package com.lanyou.esb.cook.proxy.service.impl;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.lanyou.esb.cook.proxy.common.Constants;
import com.lanyou.esb.cook.proxy.common.SystemResCode;
import com.lanyou.esb.cook.proxy.dao.impl.BaseDao;
import com.lanyou.esb.cook.proxy.entity.FieldSystem;
import com.lanyou.esb.cook.proxy.exception.AuthException;
import com.lanyou.esb.cook.proxy.meta.EnableType;
import com.lanyou.esb.cook.proxy.meta.SystemType;
import com.lanyou.esb.cook.proxy.redis.proxy.impl.FieldSystemJedisProxy;
import com.lanyou.esb.cook.proxy.service.FieldSystemService;

/**
 * 领域系统服务
 * 
 * @author Davey.wu
 */
public class FieldSystemServiceImpl implements FieldSystemService {
	/** 数据库操作接口 **/
	private BaseDao<FieldSystem> dao;
	/** Redis代理接口 **/
	private FieldSystemJedisProxy jedisProxy;

	@Override
	public void loadAllToRedis() {
		// 存到Redis,不需要
		try {
			jedisProxy.loadHashAll(dao.findAll());
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public FieldSystem getSystemByCode(String code) {
		try {
			return jedisProxy.hget(jedisProxy.getKey(code));
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void reload(FieldSystem system) {
		// TODO Auto-generated method stub
		try {
			jedisProxy.reloadHash(system);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public FieldSystemJedisProxy getJedisProxy() {
		return jedisProxy;
	}

	public void setJedisProxy(FieldSystemJedisProxy jedisProxy) {
		this.jedisProxy = jedisProxy;
	}

	@Override
	public void validate(FieldSystem system, SystemType type)
			throws AuthException {
		if (system == null) {
			return;
		}
		if (system.isEnable()) {
			return;
		}
		// 系统不可用
		if (TraceLogServiceImpl.isTrace()) {
			// 记录跟踪日志
			TraceLogServiceImpl.addTraceLog(SystemResCode.MSG_IF_DISABLE,
					Constants.FLAG_LEFT_SQUARE_BRACKET, Constants.IS_ENABLE,
					Constants.FLAG_EQUAL, EnableType.DISABLE.ValueCode,
					Constants.FLAG_RIGHT_SQUARE_BRACKET);
		}
		if (SystemType.PROVIDER == type) {
			throw new AuthException(SystemResCode.MSG_PUB_SYSTEM_DISABLE,
					SystemResCode.CODE_PUB_SYSTEM_DISABLE);
		}
		throw new AuthException(SystemResCode.MSG_CALL_SYSTEM_DISABLE,
				SystemResCode.CODE_CALL_SYSTEM_DISABLE);
	}

	public BaseDao<FieldSystem> getDao() {
		return dao;
	}

	public void setDao(BaseDao<FieldSystem> dao) {
		this.dao = dao;
	}

}
